
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.microblog.dao.CollectionDao;
import org.microblog.entity.Blog;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;

public class CollectionDaoTest {
	SqlSession sqlSession;

	@Before
	public void before(){
		InputStream is = null;
		try {
			is = Resources.getResourceAsStream("mybatis-config.xml");
			SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
			sqlSession =  factory.openSession();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@After
	public void after(){
		if (sqlSession!=null){
			sqlSession.close();
		}
	}

	@Test
	public void testPage(){
		CollectionDao collectionDao = sqlSession.getMapper(CollectionDao.class);
		try {
			 List<Blog> list = collectionDao.getCollectionsByPage(1, null, 0, 10);
			 for (Blog tmp : list){
				 System.out.println(tmp.getUser().getNickname()+" "+tmp.getContent()+" ");
			 	if (tmp.getOrigin()!=null) {
				    System.out.print("转发自："+tmp.getUser().getNickname());
			    }
			 }

		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}
	}
}
